package com.fbt.transfer.app.driver.http;

import android.text.TextUtils;
import com.fbt.transfer.app.driver.MyApplication;
import com.fbt.transfer.app.driver.util.AppInfoUtils;
import com.fbt.transfer.app.driver.util.Constants;
import com.fbt.transfer.app.driver.util.UserManager;
import java.io.IOException;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;
import org.jetbrains.annotations.NotNull;

/**
 * @author tuocoo
 */
public class CustomerHttpInterceptor implements Interceptor {


    @NotNull
    @Override
    public Response intercept(Chain chain) throws IOException {
        Request originalRequest = chain.request();
        // 获取保存的token
        String token = UserManager.getInstance(MyApplication.getApplication()).getToken();
        // 如果token存在，将其添加到请求头中
        if (!TextUtils.isEmpty(token)) {
            Request newRequest = originalRequest.newBuilder()
                .header(Constants.TOKEN_HEADER_NAME, token)
                .header(Constants.USER_AGENT, AppInfoUtils.getUserAgent())
                .build();
            return chain.proceed(newRequest);
        }
        return chain.proceed(originalRequest);
    }
} 
